package js.web.center.thread;

import java.net.InetSocketAddress;

import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.session.DefaultSessionIdManager;

import js.web.center.core.config.JsWebConfig;
import js.web.center.web.JsWebSocketChangeHandler;

/**
 * websocket服务器
 * @author qpduw
 *
 */
public class JsWebSocketServerThread implements Runnable {
	private Logger logger=Logger.getLogger(JsWebSocketServerThread.class);
	@Override
	public void run() {
		try {
			InetSocketAddress inetSocketAddress=new InetSocketAddress(JsWebConfig.webSocketBindIp, JsWebConfig.webSocket);
			Server webSocketServer=new Server(inetSocketAddress);
			JsWebSocketChangeHandler jsWebSocketChangeHandler = new JsWebSocketChangeHandler();
			ContextHandler contextHandler=new ContextHandler();
			contextHandler.setContextPath(JsWebConfig.webSocketPath);
			jsWebSocketChangeHandler.setHandler(contextHandler);
			DefaultSessionIdManager defaultSessionIdManager = new DefaultSessionIdManager(webSocketServer);
			webSocketServer.setSessionIdManager(defaultSessionIdManager);
			webSocketServer.setHandler(jsWebSocketChangeHandler);
			webSocketServer.start();
			webSocketServer.join();
		} catch (Exception e) {
			logger.error("JsWebSocketServerThread ERROR:{}",e);
		}
	}
}
